package com.borrowed.book.book.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.borrowed.book.book.domain.SShippingAddress;
import com.borrowed.book.book.mapper.SShippingAddressMapper;
import com.borrowed.book.book.service.UserAddressService;
import com.borrowed.book.system.domain.AjaxResult;
import com.borrowed.book.system.domain.Province;
import com.borrowed.book.user.domain.SUser;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ClassUtils;

import java.io.*;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserAddressServiceImpl implements UserAddressService {
    @Autowired
    private SShippingAddressMapper addressMapper;

    /**
     * 从json文件中获取省市区数据
     * @return
     */
    @Override
    public List<Province> getProvinces() {
        List<Province> provinces=null;
        try {
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("static/2020年中华人民共和国县以上行政区划代码.json");
            Gson gson = new Gson();
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
            Type type = new TypeToken<List<Province>>() {}.getType();
            provinces = gson.fromJson(br, type);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return provinces;
    }

    @Override
    public AjaxResult addShippingAddress(SShippingAddress address) {
        //现在先有前端传入微信用户openId过来，后面看看时候改为根据登录用户自动获取
        SUser sysUser = (SUser)SecurityUtils.getSubject().getPrincipal();
        if(address.getIsdefault()==1){
            //如果新加入的一条设置为默认的，那么该用户的其它地址更新为非默认的
            Map<String,Object> map=new HashMap<>();
            map.put("openId",sysUser.getOpenid());
            map.put("isDefault",0);
            addressMapper.updateIsDefaultAddressByOpenId(map);
        }
        address.setOpenid(sysUser.getOpenid());
        addressMapper.insert(address);
        return AjaxResult.sucess();
    }

    @Override
    public void deleteShippingAddressById(Long id) {
        addressMapper.deleteById(id);
    }

    /**
     * 更新收获地址
     *
     * @param address
     */
    @Override
    public void updateShippingAddressById(SShippingAddress address) {
        SUser sysUser = (SUser)SecurityUtils.getSubject().getPrincipal();
        if(address.getIsdefault()==1){
            //如果更新的一条设置为默认的，那么该用户的其它地址更新为非默认的
            Map<String,Object> map=new HashMap<>();
            map.put("openId",sysUser.getOpenid());
            map.put("isDefault",0);
            addressMapper.updateIsDefaultAddressByOpenId(map);
        }
        addressMapper.updateShippingAddress(address);
    }

    /**
     * 根据当前登录用户查询收件地址
     *
     * @return
     */
    @Override
    public List<SShippingAddress> selectShippingAddressList() {
        SUser sysUser = (SUser)SecurityUtils.getSubject().getPrincipal();

        Map<String,Object> param=new HashMap<>();
        param.put("openId",sysUser.getOpenid());
        List<SShippingAddress> addressList=addressMapper.selectShippingAddressList(param);
        return addressList;
    }
}
